<?php

namespace app\store\controller\goods;

use app\store\controller\BaseController;
use app\store\model\GoodsCategory;
use app\store\service\GoodsCategoryService;
use think\exception\ValidateException;
use think\facade\View;

class CategoryController extends BaseController
{
    public function index()
    {   
        $keyword = $this->request->param('keyword','');

        $goodsCategoryService = new GoodsCategoryService();
        $list = $goodsCategoryService->getCategoryList($keyword);


        return View::fetch('',['list' => $list]);
    }
    
    public function create()
    {
        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption();

        View::engine()->layout(false);
        

        return View::fetch('',['options' => $options]);
    }

    public function child()
    {   
        $id = $this->request->get('id/d');

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption();

        View::engine()->layout(false);

        return View::fetch('',['options' => $options,'parent_id' => $id]);
    }

    public function edit()
    {   
        $id = $this->request->get('id/d');

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption();
        $row = $goodsCategoryService->findCategory($id);

        View::engine()->layout(false);

        return View::fetch('',['options' => $options, 'row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();
        
        try{
            validate('GoodsCategoryValidate')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
        
        $param['store_id'] = session('store_id');
        $id =  GoodsCategoryService::saveGoodsCategory($param);

        return $id ? json(['code' => 1, 'msg' => '保存成功!']) : json(['code' => 0, 'msg' => '保存失败!']);
        
    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            validate('GoodsCategoryValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        
        GoodsCategory::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }

    public function home()
    {
        $param = $this->request->post();

        try {
            validate('GoodsCategoryValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        GoodsCategory::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }

    public function update()
    {
        $param = $this->request->post();
        
        try{
            validate('GoodsCategoryValidate.edit')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        $model =  GoodsCategory::update($param);

        return !empty($model['id']) ? json(['code' => 1, 'msg' => '更新成功!']) : json(['code' => 0, 'msg' => '更新失败!']);
    }


    public function delete()
    {
        $id = $this->request->get('id/d');
        
        $goodsCategoryService = new GoodsCategoryService();
        $son = $goodsCategoryService->categorySonExsits($id);

        if($son){
            return json(['code' => 0, 'msg' => '存在子分类不能删除,请先删除子分类!']);
        }

        $res = GoodsCategory::destroy($id);

        return  $res ? json(['code' => 1,'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败']);

    }
}